package com.sctvcloud.renhe.ui.activities;

import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.mob.tools.utils.DeviceHelper;
import com.ruihang.generalibrary.ui.widget.CustomFontEditText;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.application.Constances;
import com.sctvcloud.renhe.base.BaseActivity;
import com.sctvcloud.renhe.beans.IntegralAddBean;
import com.sctvcloud.renhe.beans.SingleResult;
import com.sctvcloud.renhe.http.AbsNetCallBack;
import com.sctvcloud.renhe.http.DMethods;
import com.sctvcloud.renhe.http.NetUtils;
import com.sctvcloud.renhe.ui.utils.KeyBoardUtils;
import com.sctvcloud.renhe.ui.utils.TitleUtils;
import com.sctvcloud.renhe.utils.UserManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 邀请码页
 */
public class InvitationCodeActivity extends BaseActivity {
    @BindView(R.id.invitation_code_number)
    protected CustomFontEditText invitation_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).setBackVisible(true).setLogo(false);
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_invitation_code);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.invitation_code_submit})
    public void submitClick(View view) {
        String code = invitation_code.getText().toString();
        if (code == null || code.length() == 0) {
            toast("请输入邀请码");
            return;
        }
        upLoadInvitationCode(code);
    }


    /**
     * 提交邀请码
     *
     * @param code
     */
    private void upLoadInvitationCode(String code) {
        KeyBoardUtils.closeKeyboard(this);
        showProgress(getString(R.string.being_submit), false);
        ArrayMap<String, Object> map = new ArrayMap<>();
        if (code.endsWith("XO")) {
            if (null != UserManager.getInstance().getUser()) {
                //积分商城的邀请码
                map.put("inviteCode", code);
                map.put("userId", UserManager.getInstance().getUser().getPhoneNumber());
                map.put("token", UserManager.getInstance().getUser().getToken());
                NetUtils.getNetAdapter().checkInvitationCode(getOwnerName(), map, new AbsNetCallBack<SingleResult>(null) {
                    @Override
                    public void onSuc(SingleResult s) {
                        closeProgress();
                        if (s != null && s.isSuccess()) {
                            toast(getString(R.string.submit_success));
                            finish();
//                            addInvitation(Constances.ADD_INTEGRAL_INPUT_INVITATION, true);
                        } else {
                            toast(R.string.submit_failed);
                        }
                    }

                    @Override
                    public void onError(Throwable ex, String errorMsg) {
                        super.onError(ex, errorMsg);
                        if (ex != null) {
                            ex.printStackTrace();
                        }
                        toast(TextUtils.isEmpty(errorMsg) ? "提交失败" : errorMsg);
                    }

                    @Override
                    public SingleResult doInBackground(SingleResult s) {
                        return super.doInBackground(s);
                    }

                    @Override
                    public void onEnd() {
                        super.onEnd();
                        closeProgress();
                    }
                });
            }
        } else {
            //市州推广邀请码
            String did =
                    DeviceHelper.getInstance(this).getDeviceId();
            if (TextUtils.isEmpty(did)) {
                did = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
            }
            JLog.e("did=" + did);
            map.put("deviceId", did);
            map.put("inviteCode", code);
            map.put("siteId", Constances.SITE_ID);
            NetUtils.getNetAdapter().postGetSingle(getOwnerName(), DMethods.SendCode, map, new AbsNetCallBack<SingleResult>(null) {
                @Override
                public void onSuc(SingleResult s) {
                    closeProgress();
                    if (s != null && s.isSuccess()) {
                        toast(getString(R.string.submit_success));
                        finish();
//                        addInvitation(Constances.ADD_INTEGRAL_INPUT_INVITATION, true);

                    } else {
                        toast(R.string.submit_failed);
                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
                    super.onError(ex, errorMsg);
                    if (ex != null) {
                        ex.printStackTrace();
                    }
                    toast(TextUtils.isEmpty(errorMsg) ? "提交失败" : errorMsg);
                }

                @Override
                public SingleResult doInBackground(SingleResult s) {
                    return super.doInBackground(s);
                }

                @Override
                public void onEnd() {
                    super.onEnd();
                    closeProgress();
                }
            });
        }
    }


}
